package cn.jiedh.app.meiweidk.Utils.UI;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;


/**
 * @name：
 * @author： 周毅
 * @phone： 15826080712
 * @createTime： 2017/5/31
 * @modifyTime： 2017/5/31
 * @explain：
 */

public class SSODialog extends Dialog implements View.OnClickListener{
    private Activity context;      // 上下文
    private int layoutResID;      // 布局文件id
    private int[] listenedItems;  // 要监听的控件id
    private EditText etphone;
    private EditText etcode;
    private TextView tvappley;
    private MyTBView myTBView;
    private String phone;
    private String code;
    private boolean isshowappley=false;
    private ImageView close;
    private LinearLayout imagecodelayout;
    private EditText imagecodeedt;
    private ImageView imagecodeimg;
    private boolean isshowimgcode;
    private Bitmap bitmap;
    private String imgcode;
    private ArrayList buriedList;

    public SSODialog(Activity context,boolean isshowimgcode,OnCenterItemClickListener listener) {
        super(context, R.style.dialog_custom); //dialog的样式
        this.context = context;
        this.isshowimgcode = isshowimgcode;
        this.layoutResID = R.layout.ssodialog;
        this.listener=listener;
    }
    public SSODialog(Activity context, Bitmap bitmap,boolean isshowimgcode, OnCenterItemClickListener listener) {
        super(context, R.style.dialog_custom); //dialog的样式
        this.context = context;
        this.bitmap=bitmap;
        this.isshowimgcode = isshowimgcode;
        this.layoutResID = R.layout.ssodialog;
        this.listener=listener;
    }
    public void setBitmap(Bitmap bitmap){
        this.bitmap=bitmap;
        //TODO 刷新dialog
        imagecodeimg.setImageBitmap(bitmap);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
     //   window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(layoutResID);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5; // 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(false);// 点击Dialog外部消失
        //遍历控件id,添加点击事件
        myTBView=findViewById(R.id.verification_btn);
        etphone=findViewById(R.id.etphone);
        etcode=findViewById(R.id.etcode);
        close = findViewById(R.id.close);
        tvappley=findViewById(R.id.Apply);
        imagecodelayout=findViewById(R.id.image_code_layout);
        imagecodeedt = findViewById(R.id.image_code_edt);
        imagecodeimg = findViewById(R.id.image_code_img);
        if (isshowimgcode){
            imagecodeimg.setImageBitmap(bitmap);
            imagecodelayout.setVisibility(View.VISIBLE);
        }else{
            imagecodelayout.setVisibility(View.GONE);
        }
        initevent();
    }

    private void initevent() {
        imagecodeimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.RefreshImg();
            }
        });
        myTBView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(context,"newbie_getCode");
                String phone = etphone.getText().toString().trim();
                if(TextUtils.isEmpty(phone)){
                    T.showShortToast("请输入手机号");
                    return;
                }
//                if (!StringUtil.isMobileNO(phone)){
//                    T.showShortToast("请输入正确的手机号");
//                    return;
//                }
                if (isshowimgcode){
                    String imgcode =imagecodeedt.getText().toString().trim();
                    if (TextUtils.isEmpty(imgcode)){
                        T.showShortToast("请输入图形验证码");
                        return;
                    }
                }
                if (isshowimgcode){
                    listener.OnCenterItemClick(SSODialog.this,phone,code,1,myTBView,imgcode);
                }else{
                    listener.OnCenterItemClick(SSODialog.this,phone,code,1,myTBView,"");
                }
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
       if (isshowappley){
           tvappley.setEnabled(true);
           tvappley.setBackgroundResource(R.color.red);
       }else{
           tvappley.setEnabled(false);
           tvappley.setBackgroundResource(R.color.colorGray);
       }
       tvappley.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               MobclickAgent.onEvent(context,"newbie_apply");
               if (isshowimgcode){
                   listener.OnCenterItemClick(SSODialog.this,phone,code,2,myTBView,imgcode);
               }else{
                   listener.OnCenterItemClick(SSODialog.this,phone,code,2,myTBView,"");
               }
           }
       });
        etphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                buttonchange();
            }
        });
        etcode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                buttonchange();
            }
        });
        imagecodeedt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                buttonchange();
            }
        });
    }

    private void buttonchange() {
        phone =etphone.getText().toString().trim();
        code = etcode.getText().toString().trim();
        imgcode =imagecodeedt.getText().toString().trim();
        if (isshowimgcode){

            if (phone.isEmpty()||code.isEmpty()||imgcode.isEmpty()){
                isshowappley =false;
                tvappley.setEnabled(false);
                tvappley.setBackground(context.getResources().getDrawable(R.drawable.shape_gray1_bg));
            }else{
                isshowappley=true;
                tvappley.setEnabled(true);
                tvappley.setBackground(context.getResources().getDrawable(R.drawable.shape_btn_red));
            }
        }else{
            if (phone.isEmpty()||code.isEmpty()){
                isshowappley =false;
                tvappley.setEnabled(false);
                tvappley.setBackground(context.getResources().getDrawable(R.drawable.shape_gray1_bg));
            }else{
                isshowappley=true;
                tvappley.setEnabled(true);
                tvappley.setBackground(context.getResources().getDrawable(R.drawable.shape_btn_red));
            }
        }

    }

    private OnCenterItemClickListener listener;
    public interface OnCenterItemClickListener {
        void OnCenterItemClick(SSODialog dialog,String phone,String code,int tag,MyTBView myTBView,String imgcode);
        void RefreshImg();
    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }
    @Override
    public void onClick(View view) {
       // dismiss();//注意：我在这里加了这句话，表示只要按任何一个控件的id,弹窗都会消失，不管是确定还是取消。
    }


}
